package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dto.ReportForm;
import com.entity.OrderDetail;
import com.entity.Orders;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.OrderDetailMapper;
import com.service.OrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utils.PageObject;
import com.vo.OrdersVO;
import com.vo.StorageReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  入库订单详情服务实现类
 * </p>
 *
 * @author aiou
 * @since 2025-06-10
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    //判断是否有相同的批次号
    @Override
    public boolean checkBatchNo(String batchNoStr) {
        String[] batchArray = batchNoStr.split(",");
        //todo:1、判断是否传入相同批次号
        //HashSet去重
        if (batchArray.length != new HashSet<>(Arrays.asList(batchArray)).size()) {
            // 存在重复值
            return false;
        }
        //todo:2、判断数据库中是否有重复的
        if (orderDetailMapper.checkBatchNo(batchArray).size() == 0){
            return true;
        }
        return false;
    }

    //获取订单详情
    @Override
    public List<OrderDetail> list(String orderNo) {
        return orderDetailMapper.list(orderNo);
    }

    //判断是否有相同的批次号
    @Override
    @Transactional
    public boolean checkBatchNo(String orderNo, String batchNoStr) {
        String[] batchArray = batchNoStr.split(",");
        List<OrderDetail> orderDetailList = orderDetailMapper.checkBatchNo(batchArray);
        //能查出来，说明批次号重复，则判断是否是同一个订单
        if (orderDetailList != null){
            for (OrderDetail orderDetail : orderDetailList) {
                if (!orderDetail.getOrderNo().equals(orderNo)) return false;
            }
        }
        return true;
    }

    //采购退货单：退货明细下拉框
    @Override
    public List<OrderDetail> orderDetailList() {
        return orderDetailMapper.orderDetailList();
    }

    //销售退货单：获取下拉框数据
    @Override
    public List<OrderDetail> saleReturnOrdersDetailList() {
        return this.orderDetailMapper.saleReturnOrdersDetailList();
    }

    //仓库统计：分页查询
    @Override
    public PageObject<StorageReportVO> storageReportList(PageObject pageObject, ReportForm form) {
        //开始分页
        PageHelper.startPage(pageObject.getCurrent(),pageObject.getSize());
        //查询数据
        List<StorageReportVO> list = this.orderDetailMapper.storageReportVOList(form);
        //PageHelper包装数据
        PageInfo<StorageReportVO> pageInfo = new PageInfo<>(list);
        //将数据封装进PageResult
        PageObject<StorageReportVO> StorageReportVOPageResult
                = new PageObject<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return StorageReportVOPageResult;
    }

    //仓库统计：Excel导出查询
    @Override
    public List<StorageReportVO> storageReportVOExportList() {
        return this.orderDetailMapper.storageReportVOExportList();
    }

}
